package com.zbkj.crmeb.merchant.admin.controller;

import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.store.request.CalculatePeopleRequest;
import com.zbkj.crmeb.store.request.StoreUserGroupFeatureRequest;
import com.zbkj.crmeb.store.request.StoreUserGroupRequest;
import com.zbkj.crmeb.store.response.StoreUserGroupResponse;
import com.zbkj.crmeb.store.service.StoreUserGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "用户分群")
@RestController
@RequestMapping("/api/admin/mer/store/group")
public class StoreUserGroupController extends AdminBaseController{

    @Autowired
    StoreUserGroupService storeUserGroupService;

    /**
     * 功能描述: 返回分群id与名称
     * @Param: [request]
     * @Return: com.crmeb.core.common.CommonResult<java.util.Map<java.lang.Integer,java.lang.String>>
     * @Author: wangzh
     * @Date: 2022/9/30 10:39
     */
    @GetMapping("/name")
    @ApiOperation("获取分群名称")
    public CommonResult<Map<Integer, String>> getGroupName(HttpServletRequest request){
        return CommonResult.success(storeUserGroupService.getGroupName(getMerId(request)));
    }

    /**
     * 功能描述: 返回是否保存/修改成功
     * @Param: [request, merId]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2022/9/30 13:21
     */
    @PostMapping
    @ApiOperation("保存/修改分群")
    public CommonResult<Object> saveGroup(@RequestBody StoreUserGroupRequest storeUserGroupRequest, HttpServletRequest request){
        return CommonResult.success(storeUserGroupService.saveGroup(storeUserGroupRequest, getMerId(request)));
    }

    /**
     * 功能描述: 分页查询分群信息
     * @Param: [request, merId]
     * @Return: java.util.List<com.zbkj.crmeb.store.response.StoreUserGroupResponse>
     * @Author: wangzh
     * @Date: 2022/9/30 15:15
     */
    @GetMapping
    @ApiOperation("获取分群信息")
    public CommonResult<CommonPage<StoreUserGroupResponse>> getList(PageParamRequest PageRequest, HttpServletRequest request){
        return CommonResult.success(storeUserGroupService.getList(PageRequest, getMerId(request)));
    }

    /**
     * 功能描述: 根据id删除分群
     * @Param: [groupId]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Object>
     * @Author: wangzh
     * @Date: 2022/9/30 15:40
     */
    @DeleteMapping("/{groupId}")
    @ApiOperation("删除分群")
    public CommonResult<Object> deleteGroup(@PathVariable Integer groupId){
        return CommonResult.success(storeUserGroupService.deleteGroup(groupId));
    }

    /**
     * 功能描述: 计算人数
     * @Param: [merId storeUserGroupFeatureRequestList]
     * @Return: Integer
     * @Author: duyb
     * @Date: 2022/10/08 09:58
     */
    @PostMapping("/calculate/people")
    @ApiOperation("计算人数")
    public CommonResult<Integer> calculatePeople(HttpServletRequest request,@RequestBody CalculatePeopleRequest calculatePeopleRequest){
        return CommonResult.success(storeUserGroupService.calculatePeople(getMerId(request), calculatePeopleRequest.getGroupId(),calculatePeopleRequest.getStoreUserGroupFeatureRequestList()));
    }

    /**
     * 功能描述: 根据分群id查询信息
     * @Param: [id, request]
     * @Return: com.crmeb.core.common.CommonResult<com.zbkj.crmeb.store.response.StoreUserGroupResponse>
     * @Author: wangzh
     * @Date: 2022/10/20 16:52
     */
    @GetMapping("/{id}")
    @ApiOperation("根据分群id查询信息")
    public CommonResult<StoreUserGroupResponse> getByGroupId(@PathVariable Integer id, HttpServletRequest request){
        return CommonResult.success(storeUserGroupService.getByGroupId(id, getMerId(request)));
    }

    /**
     * 功能描述: 根据分群id修改人数
     * @Param: [groupId, request]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Object>
     * @Author: wangzh
     * @Date: 2022/10/20 16:53
     */
    @PostMapping("/{groupId}")
    @ApiOperation("根据分群id修改人数")
    public CommonResult<Object> updateGroupPeopleNumById(@PathVariable Integer groupId, HttpServletRequest request){
        return CommonResult.success(storeUserGroupService.updateGroupPeopleNumById(groupId, getMerId(request)));
    }
}
